    //
//  DemoViewController.m
//  ConcurrencyStudy
//
//  Created by longhuihu on 11-6-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DemoViewController.h"

@implementation DemoViewController

@synthesize queue = _queue;
@synthesize textView = _textView;
@synthesize buttonStart = _buttonStart;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.queue = [[[NSOperationQueue alloc] init] autorelease];
		UIFont *font = [UIFont fontWithName:@"Arial" size:6];
		[self.textView  setFont:font];

		self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(clearTextView)];
	}
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

#pragma mark actions
-(IBAction)actionStart:(id)sender
{
	[self startOperation];
}

-(void)startOperation
{
}
-(NSString*)getThreadDes
{
	if ([NSThread isMainThread]) {
		return [NSString stringWithString:@"[main thread]"];
	} else {
		return [NSString stringWithString:@"[working thread]"];
	}	
}
-(void)refreshTextView:(id)appendText
{
	NSString *text = self.textView.text; 
	if (text&&[text length]>0) {
		self.textView.text = [NSString stringWithFormat:@"%@\n%@",text,appendText];
	} else {
		self.textView.text =  [NSString stringWithFormat:@"%@",appendText];
	}
}

-(void)clearTextView
{
	self.textView.text = nil;
}

-(void)showLog:(id)log
{
	NSString *text = [NSString stringWithFormat:@"%@%@",[self getThreadDes],log];
	[self performSelectorOnMainThread:@selector(refreshTextView:) withObject:text waitUntilDone:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self showLog:[NSString stringWithFormat:@"%@ %@",
				   keyPath,[change objectForKey:NSKeyValueChangeNewKey]
				   ]
	 ];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
